package com.longtengyuyun.springbootapplication;

import cn.willingxyz.restdoc.core.config.RestDocConfig;
import cn.willingxyz.restdoc.spring.filter.HttpBasicAuthFilter;
import cn.willingxyz.restdoc.springswagger2.EnableSwagger2;
import cn.willingxyz.restdoc.springswagger3.EnableSwagger3;
import cn.willingxyz.restdoc.swagger.common.SwaggerUIConfiguration;
import cn.willingxyz.restdoc.swagger2.RestDocConfigSwagger2Ext;
import cn.willingxyz.restdoc.swagger3.RestDocConfigSwagger3Ext;
import com.longtengyuyun.springbootapplication.ext.BearerOpenAPIFilter;
import com.longtengyuyun.springbootapplication.ext.TestOpenAPIFilter;
import com.longtengyuyun.springbootapplication.ext.TestSwaggerFilter;
import lombok.var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;

import java.util.Arrays;

/**
 * @author 赵云龙
 * @date 2020/05/05
 */
@SpringBootApplication
@EnableSwagger2
@EnableSwagger3
public class SpringBootApplicationApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootApplicationApplication.class, args);
    }


    @Bean
    RestDocConfig _swaggerConfig()
    {
        return RestDocConfig.builder()
                .apiTitle("rest doc title")
                .apiDescription("rest doc desc")
                .apiVersion("api version")
                .fieldPrefix("_")
                .tagDescriptionAsName(true)
                .hideEmptyController(true)
                .resolveJavaDocAsTypeName(false)
//                .httpBasicAuth(new RestDocConfig.HttpBasicAuth("restdoc","restdoc"))
                .packages(Arrays.asList("com.longtengyuyun.springbootapplication"))
                .servers(Arrays.asList(RestDocConfig.Server.builder().description("url desc").url("https://localhost:8080").build(),
                        RestDocConfig.Server.builder().description("second").url("http://localhost:8080").build()))
                .build();
    }
    @Bean
    RestDocConfigSwagger3Ext restDocConfigSwagger3Ext()
    {
        return RestDocConfigSwagger3Ext.builder()
                .openAPIFilters(Arrays.asList(new TestOpenAPIFilter(), new BearerOpenAPIFilter()))
                .build();
    }
    @Bean
    RestDocConfigSwagger2Ext restDocConfigSwagger2Ext()
    {
        return RestDocConfigSwagger2Ext.builder()
                .swaggerFilters(Arrays.asList(new TestSwaggerFilter()))
                .build();
    }
    @Bean
    SwaggerUIConfiguration _swaggerUIConfiguration()
    {
        var uiConfig = new SwaggerUIConfiguration();
//        uiConfig.setDefaultModelRendering("model");
        uiConfig.setDefaultModelExpandDepth(100);
//        uiConfig.setDocExpansion("full");
        return uiConfig;
    }

    @Bean
    @ConditionalOnClass(FilterRegistrationBean.class)
    @ConditionalOnMissingBean(RestDocConfig.HttpBasicAuth.class)
    public FilterRegistrationBean<HttpBasicAuthFilter> swagger2HttpFilter(@Autowired(required = false) RestDocConfig restDocConfig) {
        RestDocConfig.HttpBasicAuth httpBasicAuth;
        if (restDocConfig == null || (httpBasicAuth = restDocConfig.getHttpBasicAuth()) == null){
            httpBasicAuth = new RestDocConfig.HttpBasicAuth(null, null);
        }

        FilterRegistrationBean<HttpBasicAuthFilter> filterBean = new FilterRegistrationBean<>();
        HttpBasicAuthFilter authFilter = new HttpBasicAuthFilter(httpBasicAuth.getUsername(), httpBasicAuth.getPassword());
        filterBean.addUrlPatterns("/swagger2-ui/**","/swagger2.json","/swagger-ui/*","/swagger.json");
        filterBean.setFilter(authFilter);
        return filterBean;
    }

}
